<?php
class Menu_model extends CI_Model{
	
	private function Menu_id_permission(){
		$u = $this->session->userdata('admin');
		$this->db->select('p.menu_id');
		$this->db->where('p.username',$u);
		$query = $this->db->get('permissions p');
		$temp = array();
		
		foreach ($query->result_array() as $row){
			$temp[] = $row['menu_id'];
		}
		$query->free_result();
		
		return $temp;
	}
	
	/**
	 * Get all back end menu
	 * @param $lang
	 */
	public function getBackEndMenu($lang,$id_ref){
		$data = array();
		
		$menu_id = $this->Menu_id_permission();
		
		$this->db->select('m.id, ml.menu_name, m.link');
		$this->db->from('menus m');
		$this->db->where('m.public','1');
		$this->db->where('m.type','2');
		$this->db->where('m.menu_id_ref',$id_ref);
		$this->db->where_in('m.id',$menu_id);
		$this->db->order_by('m.order');
		$this->db->join('menu_languages ml','m.id = ml.menu_id');
		$this->db->join('languages l','l.id = ml.language_id AND l.code =\''.$lang. '\'');
		$q = $this->db->get();
		
		if($q->num_rows()){
			$data = $q->result_array();
		}
		
		$q->free_result();
		
		return $data;
		
	}
	
	
}